#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <math.h>

#include <est.h>
#include <va.h>

static double Calc_A(double *ma, unsigned int n);

double Estimador_MU_Normal(double *ma, unsigned int n){
	double res = 0.0, p = 1.0 / (double)n;
	unsigned int i = 0;
	
	for(i = 0 ; i < n ; i++)
		res = res + ma[i];
	
	return res*p;
}

double Estimador_V_Normal(double *ma, unsigned int n){
	double u = 0.0, p = 1.0 / (double)n, res = 0.0;
	unsigned int i = 0;
	
	u = Estimador_MU_Normal(ma, n);
	
	for(i = 0 ; i < n ; i++)
		res = res + pow(ma[i] - u, 2.0);
	
	return res*p;
}

double Estimador_MU_Log_Normal(double *ma, unsigned int n){
	double res = 0.0, p = 1 / (double)n;
	unsigned int i = 0;
	
	for(i = 0 ; i < n ; i++)
		res = res + log(ma[i]);
	
	return res*p;
}

double Estimador_V_Log_Normal(double *ma, unsigned int n){
	double u = 0.0, p = 1 / (double)n, res = 0.0;
	unsigned int i = 0;
	
	u = Estimador_MU_Log_Normal(ma, n);
	
	for(i = 0 ; i < n ; i++)
		res = res + pow(log(ma[i]) - u, 2.0);
	
	return res*p;
}


double Estimador_Alpha_Gamma(double *ma, unsigned int n){
	double A = Calc_A(ma, n);
	
	return (1.0/(4.0*A))*(1.0 + pow((1.0 + (4.0/3.0)*A) ,0.5));
}

double Estimador_Lambda_Gamma(double alpha, double *ma, unsigned int n){
	double Mx = 0.0;
	unsigned int i = 0;
	
	for(i = 0 ; i < n ; i++)
		Mx = Mx + ma[i];
	
	return (alpha/(Mx/(double)n));
}


static double Calc_A(double *ma, unsigned int n){
	double sum = 0.0, Mx = 0.0;
	unsigned int i = 0;
	
	for(i = 0 ; i < n ; i++){
		Mx = Mx + ma[i];
		sum = sum + log(ma[i]);
	}
	
	return (log(Mx/(double)n) - sum/(double)n);
}
